// API Configuration for Multi-Chain Support
import { ChainType } from './chains';

// Legacy API URL (for backward compatibility)
export const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:9000';

// Multi-chain API URLs with fallback
export const getApiBaseUrl = (chain: ChainType): string => {
  switch (chain) {
    case 'mainnet':
      return process.env.NEXT_PUBLIC_MAINNET_API_URL || 
             (process.env.NODE_ENV === 'development' ? 'http://localhost:9000' : 'https://admin.detvs.dev');
    case 'testnet':
      return process.env.NEXT_PUBLIC_TESTNET_API_URL || 
             (process.env.NODE_ENV === 'development' ? 'http://localhost:9001' : 'https://api-test.detv.media');
    default:
      return API_BASE_URL;
  }
};

export const API_VERSION = '/api/v1';

export const API_ENDPOINTS = {
  // Auth
  auth: {
    nonce: `${API_VERSION}/auth/nonce`,
    login: `${API_VERSION}/auth/login`,
    refresh: `${API_VERSION}/auth/refresh`,
    logout: `${API_VERSION}/auth/logout`,
  },
  
  // Admin
  admin: {
    profile: `${API_VERSION}/admin/me`,
    stats: `${API_VERSION}/admin/stats`,
    users: `${API_VERSION}/admin/users`,
    freezeUser: (userId: string) => `${API_VERSION}/admin/users/${userId}/freeze`,
    unfreezeUser: (userId: string) => `${API_VERSION}/admin/users/${userId}/unfreeze`,
    blockchainStatus: `${API_VERSION}/admin/blockchain/status`,
    blockchainStats: `${API_VERSION}/admin/blockchain/stats`,
    verifyTransaction: `${API_VERSION}/admin/blockchain/verify-transaction`,
    announcements: {
      list: `${API_VERSION}/admin/announcements/all`,
      stats: `${API_VERSION}/admin/announcements/stats`,
      create: `${API_VERSION}/admin/announcements/create`,
      update: (id: string) => `${API_VERSION}/admin/announcements/${id}`,
      delete: (id: string) => `${API_VERSION}/admin/announcements/${id}`,
      banners: `${API_VERSION}/admin/announcements/banners`,
      toggleBanner: (id: string) => `${API_VERSION}/admin/announcements/${id}/toggle-banner`,
      types: `${API_VERSION}/admin/announcements/types/list`,
    },
    banners: {
      list: `${API_VERSION}/admin/banners/all`,
      stats: `${API_VERSION}/admin/banners/stats`,
      create: `${API_VERSION}/admin/banners/create`,
      update: (id: string) => `${API_VERSION}/admin/banners/${id}`,
      delete: (id: string) => `${API_VERSION}/admin/banners/${id}`,
      toggle: (id: string) => `${API_VERSION}/admin/banners/${id}/toggle`,
      active: `${API_VERSION}/admin/banners/active`,
      uploadImage: `${API_VERSION}/admin/banners/upload-image`,
    },
  },
  
  // Users
  users: {
    list: `${API_VERSION}/users`,
    profile: (walletAddress: string) => `${API_VERSION}/users/profile/${walletAddress}`,
    referral: (code: string) => `${API_VERSION}/users/referral/${code}`,
  },
  
  // Miners
  miners: {
    list: `${API_VERSION}/miners`,
    details: (id: string) => `${API_VERSION}/miners/${id}`,
    rewards: (id: string) => `${API_VERSION}/miners/${id}/rewards`,
    configs: `${API_VERSION}/miners/configs/types`,
  },
  
  // DTVC
  dtvc: {
    exchangeRate: `${API_VERSION}/dtvc/exchange-rate`,
  },
  
  // Announcements
  announcements: {
    list: `${API_VERSION}/announcements`,
    details: (id: string) => `${API_VERSION}/announcements/${id}`,
    types: `${API_VERSION}/announcements/types/list`,
  },

  // Upgradeable Contracts
  contracts: {
    health: `${API_VERSION}/contracts/health`,
    info: `${API_VERSION}/contracts/info`,
    token: {
      info: `${API_VERSION}/contracts/token/info`,
      balance: (address: string) => `${API_VERSION}/contracts/token/balance/${address}`,
      mining: (address: string) => `${API_VERSION}/contracts/token/mining/${address}`,
      startMining: `${API_VERSION}/contracts/token/mining/start`,
    },
    nft: {
      info: `${API_VERSION}/contracts/nft/info`,
      user: (address: string) => `${API_VERSION}/contracts/nft/user/${address}`,
      miningBonus: (address: string) => `${API_VERSION}/contracts/nft/mining-bonus/${address}`,
    },
    vesting: {
      info: `${API_VERSION}/contracts/vesting/info`,
      beneficiary: (address: string) => `${API_VERSION}/contracts/vesting/beneficiary/${address}`,
      allSchedules: `${API_VERSION}/contracts/vesting/schedules/all`,
      release: `${API_VERSION}/contracts/vesting/release`,
      create: `${API_VERSION}/contracts/vesting/create`,
      revoke: (scheduleId: string) => `${API_VERSION}/contracts/vesting/revoke/${scheduleId}`,
    },
    transaction: (hash: string) => `${API_VERSION}/contracts/transaction/${hash}`,
    admin: {
      monitoringStart: `${API_VERSION}/contracts/admin/monitoring/start`,
      monitoringStop: `${API_VERSION}/contracts/admin/monitoring/stop`,
      monitoringStatus: `${API_VERSION}/contracts/admin/monitoring/status`,
    },
  },

  // Vesting (Legacy)
  vesting: {
    list: `${API_VERSION}/vesting`,
    beneficiary: (id: string) => `${API_VERSION}/vesting/beneficiary/${id}`,
    claims: (id: string) => `${API_VERSION}/vesting/beneficiary/${id}/claims`,
  },

  // Blockchain Events
  blockchainEvents: {
    list: `${API_VERSION}/blockchain-events/events`,
    stats: `${API_VERSION}/blockchain-events/events/stats`,
    byId: (id: string) => `${API_VERSION}/blockchain-events/events/${id}`,
    retry: (id: string) => `${API_VERSION}/blockchain-events/events/${id}/retry`,
  },

  // Contract Updates
  contractUpdates: {
    update: `${API_VERSION}/contract-updates/update`,
    state: (contractName: string) => `${API_VERSION}/contract-updates/state/${contractName}`,
    history: `${API_VERSION}/contract-updates/history`,
    request: (requestId: string) => `${API_VERSION}/contract-updates/requests/${requestId}`,
    supportedOperations: `${API_VERSION}/contract-updates/supported-operations`,
    cancel: (requestId: string) => `${API_VERSION}/contract-updates/cancel/${requestId}`,
  },
};